<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 保障对象统计报表管理
 * Class ReportHouseController
 * @package Admin\Controller
 */
class ReportHouseController extends BaseController {

    protected function fn_GetLastYears($num=5){
        $years = array();
        $year = intval(date("Y"));
        for($i=0; $i<$num; $i++){
            $years[] = $year-$i;
        }
        return $years;
    }

    private function action_GenerateReport(&$houseInfo, &$total){
        $where = array();
        $data = M("house h")
            ->join(C("DB_PREFIX")."community c on c.id = h.community_id", 'LEFT')
            ->field("c.name as comm_name, h.state as house_state, count(h.id) as num, sum(dbnum) as dbnum, sum(jtnum) as jtnum")
            ->where($where)->group("comm_name, house_state")->select();

        $community = M("community")->field("id,name")->select();
        $houseSt = array();
        foreach($community as $value){
            $where = array("community_id"=>$value['id']);
            $houseSt[$value['name']] = array("comm"=>$value['name'], "dbnum"=>0, "jtnum"=>0);

            $where['state'] = "闲置中";
            $houseSt[$value['name']]["闲置中"] = array(
                "num"=>0, "where"=>serialize($where)
            );

            $where['state'] = "租赁中";
            $houseSt[$value['name']]["租赁中"] = array(
                "num"=>0, "where"=>serialize($where)
            );

            $where = array("community_id"=>$value['id']);
            $houseSt[$value['name']]["总共"] = array(
                "num"=>0, "where"=>serialize($where)
            );
        }

        foreach ($data as $item) {
            $houseSt[$item['comm_name']][$item['house_state']]["num"] += $item["num"];
            $houseSt[$item['comm_name']]['总共']["num"] += $item["num"];
            $houseSt[$item['comm_name']]["dbnum"] += $item["dbnum"];
            $houseSt[$item['comm_name']]["jtnum"] += $item["jtnum"];
            $total['dbnum'] += $item["dbnum"];
            $total['jtnum'] += $item["jtnum"];
            $total[$item['house_state']] += $item["num"];
            $total["总共"] += $item["num"];
        }

        foreach ($houseSt as $key => $value) {
            $value['name'] = $key;
            $houseInfo[] = $value;
        }
    }

    public function ajax_export(){

        header("Content-type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=房源统计报表.xls");  //文件下载

        $houseInfo = array();
        $total = array("闲置中"=>0, "租赁中"=>0, "总共"=>0);
        $this->action_GenerateReport($houseInfo, $total);

        echo '<table width="200" border="1">
                  <tr>
                    <td>统计小区</td>
                    <td>低保人口</td>
                    <td>家庭人口</td>
                    <td>空置房源数量</td>
                    <td>租赁房源数量</td>
                    <td>房源总数</td>
                  </tr>';
        foreach($houseInfo as $vo){
            $tr = "
                <tr>
                <td>{{name}}</td>
                <td>{{dbnum}}</td>
                <td>{{jtnum}}</td>
                <td>{{empty}}</td>
                <td>{{rental}}</td>
                <td>{{total}}</td>
              </tr>";
            echo  strtr($tr, [
                "{{name}}" => $vo['name'],
                "{{dbnum}}" => $vo['dbnum'],
                "{{jtnum}}" => $vo['jtnum'],
                "{{empty}}" => $vo['闲置中']['num'],
                "{{rental}}" => $vo['租赁中']['num'],
                "{{total}}" => $vo['总共']['num'],
            ]);
        }
        $footer = "
                <tr>
                <td>总计：</td>
                <td>{{dbnum}}</td>
                <td>{{jtnum}}</td>
                <td>{{empty}}</td>
                <td>{{rental}}</td>
                <td>{{total}}</td>
              </tr>
            </table>";
        echo  strtr($footer, [
            "{{dbnum}}" => $total['dbnum'],
            "{{jtnum}}" => $total['jtnum'],
            "{{empty}}" => $total['闲置中'],
            "{{rental}}" => $total['租赁中'],
            "{{total}}" => $total['总共']]);
    }

    /**
     * 保障对象统计报表管理 -> formview
     */
    public function detail(){

        $years = $this->fn_GetLastYears();
        $this->assign("years", $years);

        $houseInfo = array();
        $total = array("家庭人口"=>0, "低保人口"=>0, "闲置中"=>0, "租赁中"=>0, "总共"=>0);
        $this->action_GenerateReport($houseInfo, $total);
        $this->assign("total", $total);
        $this->assign("houseInfo", $houseInfo);

        $this->display();
    }

    protected function fn_CalTotalApplies($data){
        $total = 0;
        foreach($data as $value){
            $total += $value;
        }
        return $total;
    }

}